package com.woniuxy.kaer.tenants.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.kaer.commons.jwtUtil.JwtUtil;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.redis.util.RedisKey;
import com.woniuxy.kaer.tenants.dto.FoundInfoDto;
import com.woniuxy.kaer.tenants.dto.FounderDto;
import com.woniuxy.kaer.tenants.from.TenantsLoginFrom;
import com.woniuxy.kaer.tenants.from.TenantsRegisterFrom;
import com.woniuxy.kaer.tenants.param.TenantsLoginParam;
import com.woniuxy.kaer.tenants.param.TenantsRegisterParam;
import com.woniuxy.kaer.tenants.service.FounderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 程诗洋
 * @since 2025年02月17日
 */

@RestController
@RequestMapping("/founder")
@Api(tags = "商家注册登录信息类")
public class FounderController {
    @Resource
    private FounderService founderService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/sendRegisterCode")
    @ApiOperation("发送注册验证码")
    public Result sendRegisterCode(String mobile)throws Exception{
        System.out.println(mobile);
        founderService.sendRegisterCode(mobile);
        return Result.succeed("发送成功",null);
    }

    //商家的注册，生成开始的账号
    @PostMapping("/tenantsRegister")
    @ApiOperation("注册")
    public Result register(@RequestBody TenantsRegisterFrom from)throws Exception{
        TenantsRegisterParam tenantsRegisterParam = BeanUtil.toBean(from, TenantsRegisterParam.class);
        founderService.tenantsRegister(tenantsRegisterParam);
        return Result.succeed("注册成功",null);
    }
    //商户登录
    @PostMapping("/tenantsLogin")
    @ApiOperation("登录")
    public Result tenantsLogin(@RequestBody TenantsLoginFrom from, HttpServletResponse response)throws Exception{
        TenantsLoginParam tenantsLoginParam = BeanUtil.toBean(from, TenantsLoginParam.class);
        FounderDto founderDto = founderService.tenantsLogin(tenantsLoginParam);

        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(founderDto);
        //3*24*60  三天时间过期
        String accessToken = jwtUtil.createToken(stringObjectMap, 30);
        //System.out.println(accessToken);
        String refreshToken = jwtUtil.createToken(stringObjectMap, 3 * 24 * 60);

        //将短accessToken和长refreshToken存入redis里
        stringRedisTemplate.opsForValue().set(RedisKey.ACCESS_EXPIRE(accessToken),"",30, TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(RedisKey.REFRESH_ACCESS_EXPIRE(accessToken),refreshToken,3 * 24 * 60, TimeUnit.MINUTES);

        //System.out.println(stringRedisTemplate.opsForValue().get(RedisKey.REFRESH_ACCESS_EXPIRE(accessToken)));
        //通过响应头将jwt响应给客户端
        response.setHeader("token", accessToken);
        //在跨域请求访问时 必须公开该请求头 前端才能获取到
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.succeed("商户登录成功",founderDto);
    }
    //根据Id查询商户
    @GetMapping("/getFounderById")
    @ApiOperation("根据Id查询商户")
    public Result<FoundInfoDto> getFounderById(Integer founderId)throws Exception{
        FoundInfoDto founderInfoDto = founderService.getFounderById(founderId);
        return Result.succeed("查询成功",founderInfoDto);
    }

}

